/**
 * 漆水河 脚手架基础公开课
 * 2022/8/13 --- 2022/12/13
 * 有疑问可加QQ群: 809126489，后续会持续输出与大家一起成长
 */
package com.qishuihe.common.infrastructure.security.handler;

import com.alibaba.fastjson.JSONObject;
import com.qishuihe.baseinterface.basevo.vo.BaseVO;
import com.qishuihe.baseinterface.utils.WebResponseUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        BaseVO baseVO = new BaseVO();
        baseVO.setCode(HttpStatus.METHOD_NOT_ALLOWED.value());
        baseVO.setMessage("没有权限访问，请找管理员授权");
        WebResponseUtils.renderResponse(httpServletResponse, JSONObject.toJSONString(baseVO));

    }
}
